package com.yu.security;

import com.yu.domain.Role;
import com.yu.domain.User;
import com.yu.mapper.UserMapper;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 这个UserDetailsService可以使用，但是我已经使用了UserServiceImpl这个UserDetailsService的实现类了
 * @Author : yu
 * Date : 4/27/2020 2:34 PM
 */
public class CustomUserService implements UserDetailsService {

    @Resource
    UserMapper userMapper;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userMapper.findByUserName(username);
        if (user==null){
            throw new UsernameNotFoundException("用户名不存在");
        }
        return user;
//        List<SimpleGrantedAuthority> authorities = new ArrayList<>();
//        //用于添加用户的权限，只要把用户权限添加到authorities上就行
//        for (Role role : user.getRoles()) {
//            authorities.add(new SimpleGrantedAuthority(role.getRoleName()));
//        }

    }
}
